package com.zlsy.springbootdemo.encryption.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author zhouliang
 * @date 2020/12/17 9:59
 */
public class SignUtil {
  private static final String Base16 = "0123456789ABCDEF";
  /**
   * MD5算法
   *
   * @param content 报文内容
   * @param charset 编码方式
   * @param keys 签名key：菜鸟为CP生成。签名内容为报文内容+签名Key
   * @return sign： base64编码（MD5（签名内容为报文内容+签名Key））
   */
  public static String doSignByMd5(String content, String charset, String keys) {
    String sign = "";
    content = content + keys;
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(content.getBytes(charset));
      sign = new String(Base64.encodeBase64(md.digest()), charset);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return sign;
  }

  public static String doSignByMd5Wlb(String content, String charset, String keys) {
    String sign = "";
    content = content + keys;
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(content.getBytes(charset));
      byte b[] = md.digest();
      int i;
      StringBuffer buf = new StringBuffer("");
      for (int offset = 0; offset < b.length; offset++) {
        i = b[offset];
        if (i < 0) {
          i += 256;
        }
        if (i < 16) {
          buf.append("0");
        }
        buf.append(Integer.toHexString(i));
      }
      sign = new String(Base64.encodeBase64(buf.toString().getBytes(charset)), charset);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return sign;
  }

  public static String doSignByMd5Hwjy(String content, String charset, String keys) {
    byte[] bytes = null;
    MessageDigest md;
    String sign = "";
    content = content + keys;
    try {
      md = MessageDigest.getInstance("MD5");
      bytes = md.digest(content.getBytes(charset));
      StringBuilder signuilder = new StringBuilder();
      for (byte aByte : bytes) {
        String hex = Integer.toHexString(aByte & 0xFF);
        if (hex.length() == 1) {
          signuilder.append("0");
        }
        signuilder.append(hex.toUpperCase());
      }
      sign = signuilder.toString();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
    return sign;
  }

  public static String doSignMd5Base16(String content, String charset, String keys) {
    String sign = "";
    content = content + keys;
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(content.getBytes(charset));
      sign = toString(md.digest());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return sign;
  }

  public static final String toString(byte[] b) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    for (byte item : b) {
      short value = (short) (item & 0xFF);
      byte high = (byte) (value >> 4);
      byte low = (byte) (value & 0xF);
      os.write(Base16.charAt(high));
      os.write(Base16.charAt(low));
    }
    return new String(os.toByteArray());
  }
}
